﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class TableObject
    {
        public static Table[] GetTables()
        {
            return Query(null);
        }

        public static Table GetTable(int id)
        {
            Table table = new Table();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TableDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable tables = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            table.Id                          = Convert.ToInt32(tables.Rows[0][FactoryDesc.Id]);
            table.TableCategoryId             = Convert.ToInt32(tables.Rows[0][TableDesc.TableCategoryId]);
            table.TablePropertyId             = Convert.ToInt32(tables.Rows[0][TableDesc.TablePropertyId]);
            table.RoomId                      = Convert.ToInt32(tables.Rows[0][TableDesc.RoomId]);
            table.Name                        = Convert.ToString(tables.Rows[0][FactoryDesc.Name]);
            table.SeatCount                   = Convert.ToInt32(tables.Rows[0][TableDesc.SeatCount]);
            table.BusinessState               = (TableState)Convert.ToUInt16(tables.Rows[0][TableDesc.BusinessState]);
            table.Remark                      = Convert.ToString(tables.Rows[0][TableDesc.Remark]);

            return table;
        }

        public static Table GetTable(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableDesc.Name, ConditionExpress.EQUALS, name);

            Table[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Table[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + TableDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                Table[] tables = new Table[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Table _table = new Table();

                    _table.Id                     = Convert.ToInt32(table.Rows[i][FactoryDesc.Id]);
                    _table.TableCategoryId        = Convert.ToInt32(table.Rows[i][TableDesc.TableCategoryId]);
                    _table.TablePropertyId        = Convert.ToInt32(table.Rows[i][TableDesc.TablePropertyId]);
                    _table.RoomId                 = Convert.ToInt32(table.Rows[i][TableDesc.RoomId]);
                    _table.Name                   = Convert.ToString(table.Rows[i][FactoryDesc.Name]);
                    _table.SeatCount              = Convert.ToInt32(table.Rows[i][TableDesc.SeatCount]);
                    _table.BusinessState          = (TableState)Convert.ToUInt16(table.Rows[i][TableDesc.BusinessState]);
                    _table.Remark                 = Convert.ToString(table.Rows[i][TableDesc.Remark]);

                    tables.SetValue(_table, i);
                }

                return tables;
            }
            return null;
        }

        public static Int32 Add(Table table)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(TableDesc.TableCategoryId,               table.TableCategoryId);
            builder.InsertAdd(TableDesc.TablePropertyId,               table.TablePropertyId);
            builder.InsertAdd(TableDesc.RoomId,                        table.RoomId);
            builder.InsertAdd(TableDesc.Name,                          table.Name);
            builder.InsertAdd(TableDesc.SeatCount,                     table.SeatCount);
            builder.InsertAdd(TableDesc.BusinessState,                 table.BusinessState);
            builder.InsertAdd(TableDesc.Remark,                        table.Remark);
            builder.QueryTableName = TableDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TableDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Table table)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(TableDesc.TableCategoryId,             table.TableCategoryId);
            builder.UpdateAdd(TableDesc.TablePropertyId,             table.TablePropertyId);
            builder.UpdateAdd(TableDesc.RoomId,                      table.RoomId);
            builder.UpdateAdd(TableDesc.Name,                        table.Name);
            builder.UpdateAdd(TableDesc.SeatCount,                   table.SeatCount);
            builder.UpdateAdd(TableDesc.BusinessState,               table.BusinessState);
            builder.UpdateAdd(TableDesc.Remark,                      table.Remark);
            builder.ConditionAdd(TableDesc.Id, ConditionExpress.EQUALS, table.Id);
            builder.QueryTableName = TableDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
